﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Willson.JsbManager.Bll;
using Willson.JsbManager.Comm;
using Willson.JsbManager.BllEntity;

namespace Willson.JsbManager.UI
{
    public partial class SelectCompanyIDForm : Form
    {
        private BindingList<BEQueryCompanyInfo> list = null;
        private int company_id = 0;
        private BaseForm form = null;


        public SelectCompanyIDForm(BaseForm form)
        {
            this.form = form;
            
            InitializeComponent();
 
        }

        private void bt_query_Click(object sender, EventArgs e)
        {
            if (this.textBox_text.Text.Length == 0) return;

            if (this.radioButton_pym.Checked == true)
            {
                list = BLLFactory<BLLQuery>.Instance.QueryCompanyInfo(this.textBox_text.Text, QueryCompanyType.Pym);
            }
            else
            {
                list = BLLFactory<BLLQuery>.Instance.QueryCompanyInfo(this.textBox_text.Text, QueryCompanyType.Name);
            }

            this.dataGridView.DataSource = list;
            this.dataGridView.Focus();
        }

        private void textBox_text_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) bt_query_Click(sender, e);
        }

        private void radioButton_pym_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox_text.Text = string.Empty;
            this.textBox_text.Focus();
        }

        private void radioButton_name_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox_text.Text = string.Empty;
            this.textBox_text.Focus();
        }

        private void SelectCompanyIDForm_Shown(object sender, EventArgs e)
        {
            this.textBox_text.Focus();
        }

        private void SelectCompanyIDForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            form.SelectCompanyId(company_id);
        }

        private void bt_select_Click(object sender, EventArgs e)
        {
            if (list == null) return;

            if (list.Count == 0) return;

            company_id = int.Parse(dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells["id"].Value.ToString());

            Close();
        }
    }
}
